package com.itheima.shop.util;

import com.google.gson.Gson;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public final class JsonUtil {
    /**
     * 写入json到前端页面
     */
    public static void writeJson(HttpServletResponse response, Object obj) throws IOException {
        Gson gson = new Gson();
        String toJson = gson.toJson(obj);
        writeString(response, toJson);
    }

    /**
     * 写入字符串到前端页面
     */
    public static void writeString(HttpServletResponse response, String result) throws IOException {
        PrintWriter pw = response.getWriter();
        pw.write(result);
        pw.flush();
        pw.close();
    }

}
